package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.AnnualPlanWork;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AchievementAwardsMapper;
import com.ruoyi.business.domain.AchievementAwards;
import com.ruoyi.business.service.IAchievementAwardsService;

import javax.validation.Validator;

/**
 * 成果获奖Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-17
 */
@Service
public class AchievementAwardsServiceImpl extends BaseServiceImpl implements IAchievementAwardsService
{
    @Autowired
    private AchievementAwardsMapper achievementAwardsMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询成果获奖
     * 
     * @param id 成果获奖主键
     * @return 成果获奖
     */
    @Override
    public AchievementAwards selectAchievementAwardsById(Long id)
    {
        return achievementAwardsMapper.selectAchievementAwardsById(id);
    }

    /**
     * 查询成果获奖列表
     * 
     * @param achievementAwards 成果获奖
     * @return 成果获奖
     */
    @Override
    public List<AchievementAwards> selectAchievementAwardsList(AchievementAwards achievementAwards)
    {
        return achievementAwardsMapper.selectAchievementAwardsList(achievementAwards);
    }

    /**
     * 新增成果获奖
     * 
     * @param achievementAwards 成果获奖
     * @return 结果
     */
    @Override
    public int insertAchievementAwards(AchievementAwards achievementAwards)
    {
        achievementAwards.setCreateTime(DateUtils.getNowDate());
        return achievementAwardsMapper.insertAchievementAwards(achievementAwards);
    }

    /**
     * 修改成果获奖
     * 
     * @param achievementAwards 成果获奖
     * @return 结果
     */
    @Override
    public int updateAchievementAwards(AchievementAwards achievementAwards)
    {
        achievementAwards.setUpdateTime(DateUtils.getNowDate());
        return achievementAwardsMapper.updateAchievementAwards(achievementAwards);
    }

    /**
     * 批量删除成果获奖
     * 
     * @param ids 需要删除的成果获奖主键
     * @return 结果
     */
    @Override
    public int deleteAchievementAwardsByIds(Long[] ids)
    {
        return achievementAwardsMapper.deleteAchievementAwardsByIds(ids);
    }

    /**
     * 删除成果获奖信息
     * 
     * @param id 成果获奖主键
     * @return 结果
     */
    @Override
    public int deleteAchievementAwardsById(Long id)
    {
        return achievementAwardsMapper.deleteAchievementAwardsById(id);
    }

    @Override
    public String importData(List<AchievementAwards> userList, boolean updateSupport, String operName) {
        if (userList == null || userList.isEmpty()) {
            throw new ServiceException("导入成就奖励数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();

        int i = 1;
        for (AchievementAwards award : userList) {
            try {
                i++;
                if (award.getAwardYear() == null || award.getAwardYear().equals("") || award.getEmployeeId() ==null || award.getEmployeeId().equals("") || !isAwardYearValid(award.getAwardYear())) {
                    failureNums.add(String.valueOf(i));
                    failureNum++;
                    continue ;
                }
                // 验证是否存在这个成就奖励
                AchievementAwards achievementAwards = new AchievementAwards();
                achievementAwards.setEmployeeId(award.getEmployeeId());

                List<AchievementAwards>list = achievementAwardsMapper.selectAchievementAwardsList(achievementAwards);

                ;
                if (true) {
                    // 验证数据
                    BeanValidators.validateWithException(validator, award);
                    // 设置初始密码或其他默认值
                    award.setCreateTime(DateUtils.getNowDate());
                    award.setCreateBy(operName);
                    achievementAwardsMapper.insertAchievementAwards(award);
                    successNum++;
                    //successMsg.append("<br/>" + successNum + "、成就奖励 " + award.getId() + " 导入成功");
                } else if (true) {
                    AchievementAwards existingAward = list.get(0);
                    // 验证数据
                    BeanValidators.validateWithException(validator, award);
                    award.setId(existingAward.getId());
                    award.setUpdateTime(DateUtils.getNowDate());
                    award.setUpdateBy(operName);
                    achievementAwardsMapper.updateAchievementAwards(award);
                    successNum++;
                    //successMsg.append("<br/>" + successNum + "、成就奖励 " + award.getId() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、成就奖励 " + award.getId() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、成就奖励 " + award.getId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
            //throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        setCacheInfo("成果获奖信息",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

        return successMsg.toString() + failureMsg.toString();
    }

}
